package com.yxk.bootvueshop.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxk.bootvueshop.common.CommonResult;
import com.yxk.bootvueshop.common.MangerPage;
import com.yxk.bootvueshop.common.StatusCode;
import com.yxk.bootvueshop.config.ServiceException;
import com.yxk.bootvueshop.entity.dto.SpManagerDTO;
import com.yxk.bootvueshop.entity.vo.ManagerVO;
import com.yxk.bootvueshop.service.SpManagerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("users")
public class MangerController {

    @Resource
    private SpManagerService spManagerService;

    /**
     * 分页查询管理员
     *
     * @param pagenum  当前页数
     * @param pagesize 每页大小
     * @param query    查询参数  可以为空（查询所有）
     * @return
     */
    @GetMapping
    public CommonResult<MangerPage> managerVOPage(Integer pagenum, Integer pagesize, String query) {
        Page<ManagerVO> page = new Page<>();
        page.setCurrent(pagenum);
        page.setSize(pagesize);
        //mybatis分页对象
        Page<ManagerVO> mangerByLike = spManagerService.getMangerByLike(page, query);
        //封装Manger的分页
        MangerPage mangerPage = new MangerPage(mangerByLike.getTotal(), mangerByLike.getCurrent(), mangerByLike.getRecords());
        if (ObjectUtil.isEmpty(mangerPage)) {
            return new CommonResult(null, "获取管理员列表失败", StatusCode.BADREQUEST);
        }
        //封装统一返回结果
        CommonResult<MangerPage> result = new CommonResult<>(mangerPage, "获取管理员列表成功", StatusCode.OK);
        return result;
    }

    /**
     * 添加管理员
     *
     * @param spManagerDTO
     * @return
     */
    @PostMapping
    public CommonResult<ManagerVO> create(@RequestBody SpManagerDTO spManagerDTO) {
        ManagerVO managerVO = spManagerService.create(spManagerDTO);
        if (ObjectUtil.isEmpty(managerVO)) {
            return new CommonResult<>(null, "创建失败", StatusCode.BADREQUEST);
        }
        return new CommonResult<>(managerVO, "创建成功", StatusCode.CREATED);
    }

    /**
     * 修改用户状态
     *
     * @param uId  用户 ID
     * @param type 用户状态 值为 true 或者 false
     * @return
     */
    @PutMapping("/{uId}/state/{type}")
    CommonResult<ManagerVO> updateState(@PathVariable Integer uId, @PathVariable String type) {
        ManagerVO managerVO = spManagerService.updateState(uId, type);
        if (ObjectUtil.isEmpty(managerVO)) {
            return new CommonResult<>(null, "设置状态失败", StatusCode.BADREQUEST);
        }
        return new CommonResult<>(managerVO, "设置状态成功", StatusCode.OK);
    }

    /**
     * 编辑用户提交
     *
     * @param id  用户 id
     * @param dto
     * @return
     */
    @PutMapping("/{id}")
    CommonResult<ManagerVO> updateManger(@PathVariable Integer id, @RequestBody SpManagerDTO dto) {
        ManagerVO managerVO = spManagerService.updateManger(id, dto);
        if (ObjectUtil.isEmpty(managerVO)) {
            return new CommonResult<>(managerVO, "更新失败", StatusCode.INTERNAL_SERVER_ERROR);
        }
        return new CommonResult<>(managerVO, "更新成功", StatusCode.OK);
    }

    /**
     * 根据 ID 查询用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    CommonResult<ManagerVO> getById(@PathVariable Integer id) {
        ManagerVO managerVO = spManagerService.getById(id);
        if (ObjectUtil.isEmpty(managerVO)) {
            return new CommonResult<>(managerVO, "查询失败", StatusCode.BADREQUEST);
        }
        return new CommonResult<>(managerVO, "查询成功", StatusCode.OK);
    }

    /**
     * 删除单个用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    CommonResult deleteById(@PathVariable Integer id) {
        int i = spManagerService.deleteById(id);
        if (i != 1) {
            return new CommonResult<>(null, "删除失败", StatusCode.BADREQUEST);
        }
        return new CommonResult<>(null, "删除成功", StatusCode.OK);
    }

    @PutMapping("/{id}/role")
    CommonResult<ManagerVO> updateRole(@PathVariable Integer id,@RequestBody SpManagerDTO dto) {
        ManagerVO managerVO = spManagerService.updateRole(id,dto);
        if (ObjectUtil.isEmpty(managerVO)) {
            return new CommonResult<>(managerVO, "设置角色失败", StatusCode.BADREQUEST);
        }
        return new CommonResult<>(managerVO, "设置角色成功", StatusCode.OK);
    }
}
